package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.exception.LogicException;
import cn.wolfcode.wolf2w.redis.service.IUserInfoRedisService;
import cn.wolfcode.wolf2w.repository.UserInfoRepository;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.AssertUtil;
import cn.wolfcode.wolf2w.util.Consts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.UUID;

@Service
public class UserInfoServiceImpl implements IUserInfoService {

    @Autowired
    private UserInfoRepository userInfoRepository;
    @Autowired
    private IUserInfoRedisService userInfoRedisService;

    @Override
    public void save(UserInfo userInfo) {
        userInfo.setId(null);
        userInfoRepository.save(userInfo);
    }

    @Override
    public void update(UserInfo userInfo) {
        userInfoRepository.save(userInfo);
    }

    @Override
    public void delete(String id) {
        userInfoRepository.deleteById(id);
    }

    @Override
    public UserInfo get(String id) {
        return userInfoRepository.findById(id).orElse(null);
    }

    @Override
    public List<UserInfo> listAll() {
        return userInfoRepository.findAll();
    }

    @Override
    public boolean checkPhone(String phone) {
        UserInfo userInfo = userInfoRepository.findByPhone(phone);
        return userInfo !=null;
    }

    @Override
    public void sendVerifyCode(String phone,String smsUrl,String appkey) {
        //创建验证码
        String code = UUID.randomUUID().toString().replaceAll("-", "").substring(0, 4);
        StringBuffer sb = new StringBuffer(80);
        sb.append("请在").append(Consts.VERIFY_CODE_VAI_TIME).append("分钟内使用验证码:").append(code);
        System.out.println(sb.toString());

        /*//spring提供的发送http请求的类
        RestTemplate token = new RestTemplate();
        //请求参数
        String status = token.getForObject(smsUrl, String.class,phone,"【狼行天下】"+sb.toString(),appkey);
        System.out.println(status);
        if (!status.contains("Success")){
            throw new LogicException("短信发送失败");
        }*/

        //将验证码缓存到redis
        userInfoRedisService.setVerifyCode(phone,code);
    }

    @Override
    public void regist(String phone, String nickname, String password, String rpassword, String verifyCode) {
        //判断参数是否为空
        AssertUtil.hasLength(phone,"手机号不能为空");
        AssertUtil.hasLength(nickname,"用户名不能为空");
        AssertUtil.hasLength(password,"密码不能为空");
        AssertUtil.hasLength(rpassword,"确认密码不能为空");
        AssertUtil.hasLength(verifyCode,"验证码不能为空");

        //判断两次密码是否一致
        AssertUtil.isEqueal(password,rpassword,"两次输入密码不一致");

        //判断手机号是否唯一
        if (this.checkPhone(phone)){
            throw new LogicException("手机号已注册");
        }

        //验证码是否正确
        String code = userInfoRedisService.getVerifyCode(phone);
        if (code == null || !code.equalsIgnoreCase(verifyCode)){
            throw new  LogicException("验证码已过期");
        }

        //注册
        UserInfo userInfo = new UserInfo();
        userInfo.setPhone(phone);
        userInfo.setNickname(nickname);
        userInfo.setPassword(password);
        userInfo.setEmail(null);
        userInfo.setGender(UserInfo.GENDER_SECRET);
        userInfo.setCity("广州");
        userInfo.setHeadImgUrl("/image/default.jpg");
        userInfo.setInfo("广州最靓的仔");
        userInfo.setState(UserInfo.STATE_NORMAL);

        this.save(userInfo);
    }

    @Override
    public UserInfo login(String username, String password) {

        //判断参数为空
        AssertUtil.hasLength(username,"用户名不能为空");
        AssertUtil.hasLength(password,"密码不能为空");

        //获取用户
        UserInfo user = userInfoRepository.findByPhone(username);
        if (user ==null || !password.equals(user.getPassword())){
            throw new LogicException("账号或密码错误");
        }
        return user;
    }

    @Override
    public List<UserInfo> findByCity(String keyword) {

        return userInfoRepository.findByCity(keyword);
    }


}
